﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_serviceupdate : System.Web.UI.Page
    {
        private int sid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                sid = Int32.Parse(Request.QueryString["sid"].ToString());
            }
            catch 
            {
                Response.Redirect("admin_service.aspx");
            }

            if(!IsPostBack){
                BindData(sid);
            }
            imgPicUrl.ImageUrl = hfPicUrl.Value;
        }
        private void BindData(int ServiceID)
        {
            BindServiceLevel();
            BindNotice();
            BindServiceInfo(ServiceID);
            BindServiceCategory();
        }
        private void BindNotice()
        {
            noticesuccess.Visible = false;
            noticefail.Visible = false;
            noticeNameSame.Visible = false;
            noticeUrlType.Visible = false;
        }

        private void BindServiceLevel()
        {
            List<int> level = new List<int>();
            for (int i = 0; i <= 100; i++)
            {
                level.Add(i);
            }
            ddlLevel.DataSource = level;
            ddlLevel.DataBind();
        }

        private void BindServiceInfo(int ServiceID)
        {
            ServiceInfo ser = ServiceInfoLogic.GetOneServiceInfo(ServiceID);
            txbName.Text = ser.ServiceName;
            txbURL.Text = ser.ServiceURL;
            ddlCategory.SelectedValue = ser.CategoryID.ToString();
            ddlLevel.SelectedValue = ser.ServiceLevel.ToString();
            hfPicUrl.Value = ser.ServicePicURL;
            hfServiceID.Value = ser.ServiceID.ToString();
        }
        private void BindServiceCategory()
        {
            List<ServiceCategoryInfo> sc = ServiceCategoryLogic.GetAllServiceCategoryInfo();
            ddlCategory.DataSource = sc;
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            ServiceInfo ser = createServiceInfo();
            try
            {
                ser.ServiceID = Int32.Parse(hfServiceID.Value);
            }
            catch { return; }
            int isSuccess = 0;
            isSuccess = ServiceInfoLogic.UpdateServiceInfo(ser);
            if (isSuccess != 1)
            {
                ChangeStatus(false);
            }
            else
            {
                ChangeStatus(true);
            }
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "initCloseNotice();initImgFromHidden();", true);
        }

        private ServiceInfo createServiceInfo()
        {
            ServiceInfo ser = new ServiceInfo();
            ser.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            ser.ServiceLevel = Int32.Parse(ddlLevel.SelectedValue);
            ser.ServiceName = txbName.Text.Trim().Replace("'", "''");
            ser.ServiceURL = txbURL.Text.Trim().Replace("'", "''");
            ser.ServiceTime = DateTime.Now;
            ser.ServicePicURL = hfPicUrl.Value;
            return ser;
        }
    }
}
